﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Codeology.Auctioneer
{

    public struct AuctioneerMoney
    {

        private static AuctioneerMoney empty;

        private int value;

        public AuctioneerMoney(int gold, int silver, int copper)
        {
            if (copper > 99) throw new Exception("Copper value is beyond maximum allowed.");
            if (silver > 99) throw new Exception("Silver amount is beyond maximum allowed.");

            value = int.Parse(gold.ToString() + silver.ToString("D2") + copper.ToString("D2"));
        }

        public AuctioneerMoney(int amount)
        {
            value = amount;
        }

        #region Methods

        public override string ToString()
        {
            // Explode value
            int gold, silver, copper;

            Explode(out gold,out silver,out copper);

            // Return string
            return gold.ToString() + "g " + silver.ToString("D2") + "s " + copper.ToString("D2") + "c";
        }

        private void Explode(out int gold, out int silver, out int copper)
        {
            // Initialize results
            gold = 0;
            silver = 0;
            copper = 0;

            // Check value
            if (value == 0) return;

            // Convert value to string
            string buffer = value.ToString();

            // Parse string into blocks
            List<string> blocks = new List<string>();
            string block = String.Empty;

            for(int i = (buffer.Length - 1); i >= 0; i--) {
                block = buffer[i] + block;

                if (block.Length == 2 || i == 0) {
                    blocks.Add(block);
                    block = String.Empty;
                }
            }

            // Return based on the number of blocks
            if (blocks.Count == 1) {
                copper = int.Parse(blocks[0]);
            } else if (blocks.Count == 2) {
                copper = int.Parse(blocks[0]);
                silver = int.Parse(blocks[1]);
            } else {
                copper = int.Parse(blocks[0]);
                silver = int.Parse(blocks[1]);

                string ret_val = String.Empty;

                for(int i = 2; i < blocks.Count; i++) ret_val = blocks[i] + ret_val;

                gold = int.Parse(ret_val);
            }
        }

        #endregion

        #region Static Properties

        public static AuctioneerMoney Empty
        {
            get {
                return new AuctioneerMoney(0);
            }
        }

        #endregion

        #region Properties

        public int Value
        {
            get {
                return value;
            }
        }

        public int Gold
        {
            get {
                int gold, silver, copper;

                Explode(out gold,out silver,out copper);

                return gold;
            }
        }

        public int Silver
        {
            get {
                int gold, silver, copper;

                Explode(out gold,out silver,out copper);

                return silver;
            }
        }

        public int Copper
        {
            get {
                int gold, silver, copper;

                Explode(out gold,out silver,out copper);

                return copper;
            }
        }

        #endregion

    }

}
